/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistence;

import entityBeans.Specimen;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entityBeans.Taxonomicelement;
import entityBeans.Recordlevelelement;
import entityBeans.Occurrenceelement;
import entityBeans.Monitoring;
import entityBeans.Localityelement;
import entityBeans.Identificationelement;
import entityBeans.Geospatialelement;
import entityBeans.Eventelement;
import entityBeans.Curatorialelement;
import entityBeans.Interaction;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import persistence.exceptions.NonexistentEntityException;

/**
 *
 * @author allankv
 */
public class SpecimenJpaController {

    public SpecimenJpaController() {
        emf = Persistence.createEntityManagerFactory("bdd-javaPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Specimen specimen) {
        if (specimen.getInteractionList() == null) {
            specimen.setInteractionList(new ArrayList<Interaction>());
        }
        if (specimen.getInteractionList1() == null) {
            specimen.setInteractionList1(new ArrayList<Interaction>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Taxonomicelement taxonomicelement = specimen.getTaxonomicelement();
            if (taxonomicelement != null && taxonomicelement.getIdtaxonomicelement()!=null) {
                taxonomicelement = em.getReference(taxonomicelement.getClass(), taxonomicelement.getIdtaxonomicelement());
                specimen.setTaxonomicelement(taxonomicelement);
            }
            Recordlevelelement recordlevelelement = specimen.getRecordlevelelement();
            if (recordlevelelement != null && recordlevelelement.getIdrecordlevelelement()!=null) {
                recordlevelelement = em.getReference(recordlevelelement.getClass(), recordlevelelement.getIdrecordlevelelement());
                specimen.setRecordlevelelement(recordlevelelement);
            }
            Occurrenceelement occurrenceelement = specimen.getOccurrenceelement();
            if (occurrenceelement != null && occurrenceelement.getIdoccurrenceelement()!=null) {
                occurrenceelement = em.getReference(occurrenceelement.getClass(), occurrenceelement.getIdoccurrenceelement());
                specimen.setOccurrenceelement(occurrenceelement);
            }
            Monitoring monitoring = specimen.getMonitoring();
            if (monitoring != null && monitoring.getIdmonitoring()!=null) {
                monitoring = em.getReference(monitoring.getClass(), monitoring.getIdmonitoring());
                specimen.setMonitoring(monitoring);
            }
            Localityelement localityelement = specimen.getLocalityelement();
            if (localityelement != null && localityelement.getIdlocalityelement()!=null) {
                localityelement = em.getReference(localityelement.getClass(), localityelement.getIdlocalityelement());
                specimen.setLocalityelement(localityelement);
            }
            Identificationelement identificationelement = specimen.getIdentificationelement();
            if (identificationelement != null && identificationelement.getIdidentificationelement()!=null) {
                identificationelement = em.getReference(identificationelement.getClass(), identificationelement.getIdidentificationelement());
                specimen.setIdentificationelement(identificationelement);
            }
            Geospatialelement geospatialelement = specimen.getGeospatialelement();
            if (geospatialelement != null && geospatialelement.getIdgeospatialelement()!=null) {
                geospatialelement = em.getReference(geospatialelement.getClass(), geospatialelement.getIdgeospatialelement());
                specimen.setGeospatialelement(geospatialelement);
            }
            Eventelement eventelement = specimen.getEventelement();
            if (eventelement != null && eventelement.getIdeventelement()!=null) {
                eventelement = em.getReference(eventelement.getClass(), eventelement.getIdeventelement());
                specimen.setEventelement(eventelement);
            }
            Curatorialelement curatorialelement = specimen.getCuratorialelement();
            if (curatorialelement != null && curatorialelement.getIdcuratorialelement()!=null) {
                curatorialelement = em.getReference(curatorialelement.getClass(), curatorialelement.getIdcuratorialelement());
                specimen.setCuratorialelement(curatorialelement);
            }
            List<Interaction> attachedInteractionList = new ArrayList<Interaction>();
            for (Interaction interactionListInteractionToAttach : specimen.getInteractionList()) {
                interactionListInteractionToAttach = em.getReference(interactionListInteractionToAttach.getClass(), interactionListInteractionToAttach.getIdinteraction());
                attachedInteractionList.add(interactionListInteractionToAttach);
            }
            specimen.setInteractionList(attachedInteractionList);
            List<Interaction> attachedInteractionList1 = new ArrayList<Interaction>();
            for (Interaction interactionList1InteractionToAttach : specimen.getInteractionList1()) {
                interactionList1InteractionToAttach = em.getReference(interactionList1InteractionToAttach.getClass(), interactionList1InteractionToAttach.getIdinteraction());
                attachedInteractionList1.add(interactionList1InteractionToAttach);
            }
            specimen.setInteractionList1(attachedInteractionList1);
            em.persist(specimen);
            if (taxonomicelement != null && taxonomicelement.getIdtaxonomicelement()!=null) {
                taxonomicelement.getSpecimenList().add(specimen);
                taxonomicelement = em.merge(taxonomicelement);
            }
            if (recordlevelelement != null && recordlevelelement.getIdrecordlevelelement()!=null) {
                recordlevelelement.getSpecimenList().add(specimen);
                recordlevelelement = em.merge(recordlevelelement);
            }
            if (occurrenceelement != null && occurrenceelement.getIdoccurrenceelement()!=null) {
                occurrenceelement.getSpecimenList().add(specimen);
                occurrenceelement = em.merge(occurrenceelement);
            }
            if (monitoring != null && monitoring.getIdmonitoring()!=null) {
                monitoring.getSpecimenList().add(specimen);
                monitoring = em.merge(monitoring);
            }
            if (localityelement != null && localityelement.getIdlocalityelement()!=null) {
                localityelement.getSpecimenList().add(specimen);
                localityelement = em.merge(localityelement);
            }
            if (identificationelement != null && identificationelement.getIdidentificationelement()!=null) {
                identificationelement.getSpecimenList().add(specimen);
                identificationelement = em.merge(identificationelement);
            }
            if (geospatialelement != null && geospatialelement.getIdgeospatialelement()!=null) {
                geospatialelement.getSpecimenList().add(specimen);
                geospatialelement = em.merge(geospatialelement);
            }
            if (eventelement != null && eventelement.getIdeventelement()!=null) {
                eventelement.getSpecimenList().add(specimen);
                eventelement = em.merge(eventelement);
            }
            if (curatorialelement != null && curatorialelement.getIdcuratorialelement()!=null) {
                curatorialelement.getSpecimenList().add(specimen);
                curatorialelement = em.merge(curatorialelement);
            }
            for (Interaction interactionListInteraction : specimen.getInteractionList()) {
                Specimen oldSpecimenOfInteractionListInteraction = interactionListInteraction.getSpecimen();
                interactionListInteraction.setSpecimen(specimen);
                interactionListInteraction = em.merge(interactionListInteraction);
                if (oldSpecimenOfInteractionListInteraction != null) {
                    oldSpecimenOfInteractionListInteraction.getInteractionList().remove(interactionListInteraction);
                    oldSpecimenOfInteractionListInteraction = em.merge(oldSpecimenOfInteractionListInteraction);
                }
            }
            for (Interaction interactionList1Interaction : specimen.getInteractionList1()) {
                Specimen oldSpecimen1OfInteractionList1Interaction = interactionList1Interaction.getSpecimen1();
                interactionList1Interaction.setSpecimen1(specimen);
                interactionList1Interaction = em.merge(interactionList1Interaction);
                if (oldSpecimen1OfInteractionList1Interaction != null) {
                    oldSpecimen1OfInteractionList1Interaction.getInteractionList1().remove(interactionList1Interaction);
                    oldSpecimen1OfInteractionList1Interaction = em.merge(oldSpecimen1OfInteractionList1Interaction);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Specimen specimen) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Specimen persistentSpecimen = em.find(Specimen.class, specimen.getIdspecimen());
            Taxonomicelement taxonomicelementOld = persistentSpecimen.getTaxonomicelement();
            Taxonomicelement taxonomicelementNew = specimen.getTaxonomicelement();
            Recordlevelelement recordlevelelementOld = persistentSpecimen.getRecordlevelelement();
            Recordlevelelement recordlevelelementNew = specimen.getRecordlevelelement();
            Occurrenceelement occurrenceelementOld = persistentSpecimen.getOccurrenceelement();
            Occurrenceelement occurrenceelementNew = specimen.getOccurrenceelement();
            Monitoring monitoringOld = persistentSpecimen.getMonitoring();
            Monitoring monitoringNew = specimen.getMonitoring();
            Localityelement localityelementOld = persistentSpecimen.getLocalityelement();
            Localityelement localityelementNew = specimen.getLocalityelement();
            Identificationelement identificationelementOld = persistentSpecimen.getIdentificationelement();
            Identificationelement identificationelementNew = specimen.getIdentificationelement();
            Geospatialelement geospatialelementOld = persistentSpecimen.getGeospatialelement();
            Geospatialelement geospatialelementNew = specimen.getGeospatialelement();
            Eventelement eventelementOld = persistentSpecimen.getEventelement();
            Eventelement eventelementNew = specimen.getEventelement();
            Curatorialelement curatorialelementOld = persistentSpecimen.getCuratorialelement();
            Curatorialelement curatorialelementNew = specimen.getCuratorialelement();
            List<Interaction> interactionListOld = new ArrayList<Interaction>();//persistentSpecimen.getInteractionList();
            List<Interaction> interactionListNew = new ArrayList<Interaction>();//specimen.getInteractionList();
            List<Interaction> interactionList1Old = new ArrayList<Interaction>();//persistentSpecimen.getInteractionList1();
            List<Interaction> interactionList1New = new ArrayList<Interaction>();//specimen.getInteractionList1();
            if (taxonomicelementNew != null) {
                taxonomicelementNew = em.getReference(taxonomicelementNew.getClass(), taxonomicelementNew.getIdtaxonomicelement());
                specimen.setTaxonomicelement(taxonomicelementNew);
            }
            if (recordlevelelementNew != null) {
                recordlevelelementNew = em.getReference(recordlevelelementNew.getClass(), recordlevelelementNew.getIdrecordlevelelement());
                specimen.setRecordlevelelement(recordlevelelementNew);
            }
            if (occurrenceelementNew != null) {
                occurrenceelementNew = em.getReference(occurrenceelementNew.getClass(), occurrenceelementNew.getIdoccurrenceelement());
                specimen.setOccurrenceelement(occurrenceelementNew);
            }
            if (monitoringNew != null) {
                monitoringNew = em.getReference(monitoringNew.getClass(), monitoringNew.getIdmonitoring());
                specimen.setMonitoring(monitoringNew);
            }
            if (localityelementNew != null) {
                localityelementNew = em.getReference(localityelementNew.getClass(), localityelementNew.getIdlocalityelement());
                specimen.setLocalityelement(localityelementNew);
            }
            if (identificationelementNew != null) {
                identificationelementNew = em.getReference(identificationelementNew.getClass(), identificationelementNew.getIdidentificationelement());
                specimen.setIdentificationelement(identificationelementNew);
            }
            if (geospatialelementNew != null) {
                geospatialelementNew = em.getReference(geospatialelementNew.getClass(), geospatialelementNew.getIdgeospatialelement());
                specimen.setGeospatialelement(geospatialelementNew);
            }
            if (eventelementNew != null) {
                eventelementNew = em.getReference(eventelementNew.getClass(), eventelementNew.getIdeventelement());
                specimen.setEventelement(eventelementNew);
            }
//            if (curatorialelementNew != null) {
//                curatorialelementNew = em.getReference(curatorialelementNew.getClass(), curatorialelementNew.getIdcuratorialelement());
//                specimen.setCuratorialelement(curatorialelementNew);
//            }
            List<Interaction> attachedInteractionListNew = new ArrayList<Interaction>();
            for (Interaction interactionListNewInteractionToAttach : interactionListNew) {
                interactionListNewInteractionToAttach = em.getReference(interactionListNewInteractionToAttach.getClass(), interactionListNewInteractionToAttach.getIdinteraction());
                attachedInteractionListNew.add(interactionListNewInteractionToAttach);
            }
            interactionListNew = attachedInteractionListNew;
            specimen.setInteractionList(interactionListNew);
            List<Interaction> attachedInteractionList1New = new ArrayList<Interaction>();
            for (Interaction interactionList1NewInteractionToAttach : interactionList1New) {
                interactionList1NewInteractionToAttach = em.getReference(interactionList1NewInteractionToAttach.getClass(), interactionList1NewInteractionToAttach.getIdinteraction());
                attachedInteractionList1New.add(interactionList1NewInteractionToAttach);
            }
            interactionList1New = attachedInteractionList1New;
            specimen.setInteractionList1(interactionList1New);
            specimen = em.merge(specimen);
            if (taxonomicelementOld != null && !taxonomicelementOld.equals(taxonomicelementNew)) {
                taxonomicelementOld.getSpecimenList().remove(specimen);
                taxonomicelementOld = em.merge(taxonomicelementOld);
            }
            if (taxonomicelementNew != null && !taxonomicelementNew.equals(taxonomicelementOld)) {
                taxonomicelementNew.getSpecimenList().add(specimen);
                taxonomicelementNew = em.merge(taxonomicelementNew);
            }
            if (recordlevelelementOld != null && !recordlevelelementOld.equals(recordlevelelementNew)) {
                recordlevelelementOld.getSpecimenList().remove(specimen);
                recordlevelelementOld = em.merge(recordlevelelementOld);
            }
            if (recordlevelelementNew != null && !recordlevelelementNew.equals(recordlevelelementOld)) {
                recordlevelelementNew.getSpecimenList().add(specimen);
                recordlevelelementNew = em.merge(recordlevelelementNew);
            }
            if (occurrenceelementOld != null && !occurrenceelementOld.equals(occurrenceelementNew)) {
                occurrenceelementOld.getSpecimenList().remove(specimen);
                occurrenceelementOld = em.merge(occurrenceelementOld);
            }
            if (occurrenceelementNew != null && !occurrenceelementNew.equals(occurrenceelementOld)) {
                occurrenceelementNew.getSpecimenList().add(specimen);
                occurrenceelementNew = em.merge(occurrenceelementNew);
            }
            if (monitoringOld != null && !monitoringOld.equals(monitoringNew)) {
                monitoringOld.getSpecimenList().remove(specimen);
                monitoringOld = em.merge(monitoringOld);
            }
            if (monitoringNew != null && !monitoringNew.equals(monitoringOld)) {
                monitoringNew.getSpecimenList().add(specimen);
                monitoringNew = em.merge(monitoringNew);
            }
            if (localityelementOld != null && !localityelementOld.equals(localityelementNew)) {
                localityelementOld.getSpecimenList().remove(specimen);
                localityelementOld = em.merge(localityelementOld);
            }
            if (localityelementNew != null && !localityelementNew.equals(localityelementOld)) {
                localityelementNew.getSpecimenList().add(specimen);
                localityelementNew = em.merge(localityelementNew);
            }
            if (identificationelementOld != null && !identificationelementOld.equals(identificationelementNew)) {
                identificationelementOld.getSpecimenList().remove(specimen);
                identificationelementOld = em.merge(identificationelementOld);
            }
            if (identificationelementNew != null && !identificationelementNew.equals(identificationelementOld)) {
                identificationelementNew.getSpecimenList().add(specimen);
                identificationelementNew = em.merge(identificationelementNew);
            }
            if (geospatialelementOld != null && !geospatialelementOld.equals(geospatialelementNew)) {
                geospatialelementOld.getSpecimenList().remove(specimen);
                geospatialelementOld = em.merge(geospatialelementOld);
            }
            if (geospatialelementNew != null && !geospatialelementNew.equals(geospatialelementOld)) {
                geospatialelementNew.getSpecimenList().add(specimen);
                geospatialelementNew = em.merge(geospatialelementNew);
            }
            if (eventelementOld != null && !eventelementOld.equals(eventelementNew)) {
                eventelementOld.getSpecimenList().remove(specimen);
                eventelementOld = em.merge(eventelementOld);
            }
            if (eventelementNew != null && !eventelementNew.equals(eventelementOld)) {
                eventelementNew.getSpecimenList().add(specimen);
                eventelementNew = em.merge(eventelementNew);
            }
            if (curatorialelementOld != null && !curatorialelementOld.equals(curatorialelementNew)) {
                curatorialelementOld.getSpecimenList().remove(specimen);
                curatorialelementOld = em.merge(curatorialelementOld);
            }
            if (curatorialelementNew != null && !curatorialelementNew.equals(curatorialelementOld)) {
                curatorialelementNew.getSpecimenList().add(specimen);
                curatorialelementNew = em.merge(curatorialelementNew);
            }
            for (Interaction interactionListOldInteraction : interactionListOld) {
                if (!interactionListNew.contains(interactionListOldInteraction)) {
                    interactionListOldInteraction.setSpecimen(null);
                    interactionListOldInteraction = em.merge(interactionListOldInteraction);
                }
            }
            for (Interaction interactionListNewInteraction : interactionListNew) {
                if (!interactionListOld.contains(interactionListNewInteraction)) {
                    Specimen oldSpecimenOfInteractionListNewInteraction = interactionListNewInteraction.getSpecimen();
                    interactionListNewInteraction.setSpecimen(specimen);
                    interactionListNewInteraction = em.merge(interactionListNewInteraction);
                    if (oldSpecimenOfInteractionListNewInteraction != null && !oldSpecimenOfInteractionListNewInteraction.equals(specimen)) {
                        oldSpecimenOfInteractionListNewInteraction.getInteractionList().remove(interactionListNewInteraction);
                        oldSpecimenOfInteractionListNewInteraction = em.merge(oldSpecimenOfInteractionListNewInteraction);
                    }
                }
            }
            for (Interaction interactionList1OldInteraction : interactionList1Old) {
                if (!interactionList1New.contains(interactionList1OldInteraction)) {
                    interactionList1OldInteraction.setSpecimen1(null);
                    interactionList1OldInteraction = em.merge(interactionList1OldInteraction);
                }
            }
            for (Interaction interactionList1NewInteraction : interactionList1New) {
                if (!interactionList1Old.contains(interactionList1NewInteraction)) {
                    Specimen oldSpecimen1OfInteractionList1NewInteraction = interactionList1NewInteraction.getSpecimen1();
                    interactionList1NewInteraction.setSpecimen1(specimen);
                    interactionList1NewInteraction = em.merge(interactionList1NewInteraction);
                    if (oldSpecimen1OfInteractionList1NewInteraction != null && !oldSpecimen1OfInteractionList1NewInteraction.equals(specimen)) {
                        oldSpecimen1OfInteractionList1NewInteraction.getInteractionList1().remove(interactionList1NewInteraction);
                        oldSpecimen1OfInteractionList1NewInteraction = em.merge(oldSpecimen1OfInteractionList1NewInteraction);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = specimen.getIdspecimen();
                if (findSpecimen(id) == null) {
                    throw new NonexistentEntityException("The specimen with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Specimen specimen;
            try {
                specimen = em.getReference(Specimen.class, id);
                specimen.getIdspecimen();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The specimen with id " + id + " no longer exists.", enfe);
            }
            Taxonomicelement taxonomicelement = specimen.getTaxonomicelement();
            if (taxonomicelement != null) {
                taxonomicelement.getSpecimenList().remove(specimen);
                taxonomicelement = em.merge(taxonomicelement);
            }
            Recordlevelelement recordlevelelement = specimen.getRecordlevelelement();
            if (recordlevelelement != null) {
                recordlevelelement.getSpecimenList().remove(specimen);
                recordlevelelement = em.merge(recordlevelelement);
            }
            Occurrenceelement occurrenceelement = specimen.getOccurrenceelement();
            if (occurrenceelement != null) {
                occurrenceelement.getSpecimenList().remove(specimen);
                occurrenceelement = em.merge(occurrenceelement);
            }
            Monitoring monitoring = specimen.getMonitoring();
            if (monitoring != null) {
                monitoring.getSpecimenList().remove(specimen);
                monitoring = em.merge(monitoring);
            }
            Localityelement localityelement = specimen.getLocalityelement();
            if (localityelement != null) {
                localityelement.getSpecimenList().remove(specimen);
                localityelement = em.merge(localityelement);
            }
            Identificationelement identificationelement = specimen.getIdentificationelement();
            if (identificationelement != null) {
                identificationelement.getSpecimenList().remove(specimen);
                identificationelement = em.merge(identificationelement);
            }
            Geospatialelement geospatialelement = specimen.getGeospatialelement();
            if (geospatialelement != null) {
                geospatialelement.getSpecimenList().remove(specimen);
                geospatialelement = em.merge(geospatialelement);
            }
            Eventelement eventelement = specimen.getEventelement();
            if (eventelement != null) {
                eventelement.getSpecimenList().remove(specimen);
                eventelement = em.merge(eventelement);
            }
            Curatorialelement curatorialelement = specimen.getCuratorialelement();
            if (curatorialelement != null) {
                curatorialelement.getSpecimenList().remove(specimen);
                curatorialelement = em.merge(curatorialelement);
            }
            List<Interaction> interactionList = specimen.getInteractionList();
            for (Interaction interactionListInteraction : interactionList) {
                interactionListInteraction.setSpecimen(null);
                interactionListInteraction = em.merge(interactionListInteraction);
            }
            List<Interaction> interactionList1 = specimen.getInteractionList1();
            for (Interaction interactionList1Interaction : interactionList1) {
                interactionList1Interaction.setSpecimen1(null);
                interactionList1Interaction = em.merge(interactionList1Interaction);
            }
            em.remove(specimen);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Specimen> findSpecimenEntities() {
        return findSpecimenEntities(true, -1, -1);
    }

    public List<Specimen> findSpecimenEntities(int maxResults, int firstResult) {
        return findSpecimenEntities(false, maxResults, firstResult);
    }

    private List<Specimen> findSpecimenEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Specimen.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
//    public Integer findSpecimen(String gui) {
//        Integer id = null;Query query= getEntityManager().createNamedQuery("Specimen.findByGui");
//        query.setParameter("gui",gui);
//        return query.getResultList().isEmpty()?null:(Specimen)query.getResultList().get(0);*/
//        Connection conn;
//        try {
//            Class.forName("org.postgresql.Driver");
//            conn = DriverManager.getConnection("jdbc:postgresql://10.0.10.102:5432/bdd_desenv?user=bdd_desenv&password=bdd_d3s3nv");
//            Statement stm = conn.createStatement();
//            ResultSet resultset = stm.executeQuery("SELECT idspecimen FROM Specimen s Left Join Recordlevelelement r On s.idrecordlevelelement = r.idrecordlevelelement Where r.globaluniqueidentifier = '"+gui+"'");
//            while (resultset.next()) {
//                id = resultset.getInt("idspecimen");
//            }
//        } catch (ClassNotFoundException ex) {
//            Logger.getLogger(SpecimenJpaController.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        catch (SQLException ex) {
//            Logger.getLogger(SpecimenJpaController.class.getName()).log(Level.SEVERE, null, ex);
//        }
//        return id;


//        Query query= getEntityManager().createNamedQuery("Specimen.findByGui");
//        query.setParameter("gui",gui);
//        return query.getResultList().isEmpty()?null:(Specimen)query.getResultList().get(0);*/

//    }
    public Specimen findSpecimen(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Specimen.class, id);
        } finally {
            em.close();
        }
    }

    public int getSpecimenCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Specimen> rt = cq.from(Specimen.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
